﻿namespace dogd
{
	using System.Data.Common;
	using System.Data.SQLite;

	/// <summary>
	/// Uses in-memory connection to sqlite.
	/// </summary>
	public class InMemorySqliteConnectionProvider : IConnectionProvider
	{
		/// <summary>
		/// Use an in-memory instance
		/// </summary>
		public InMemorySqliteConnectionProvider()
		{
			this.Path = ":memory:";
			this.Connection = this.Factory.CreateConnection();
			this.Connection.ConnectionString = this.ConnectionString;

			this.Connection.Open();
		}

		/// <summary>
		/// Gets a connection to the sqlite database.
		/// </summary>
		public DbConnection Connection
		{
			get;
 			private set;
		}

		private string Path { get; set; }

		private string ConnectionString
		{
			get
			{
				return string.Format("Data Source={0}", 
					this.Path, 
					System.Globalization.CultureInfo.InvariantCulture);
			}
		}

		private DbProviderFactory Factory
		{
			get
			{
				return SQLiteFactory.Instance; // DbProviderFactories.GetFactory("System.Data.SQLite");
			}
		}

	}
}
